import '@/apis/interceptors';
import axios from 'axios';
import {
  Toast
} from '@/components/AntdMobile';
// import storage from '@/utils/Storage';

export const http = {
  // get请求
  async get(path: string, params?: any, head?: any, showMsg = true) {
    let config: any = { path, params, head };
    config.method = 'get';
    config.showMsg = showMsg;

    const awaitGet = await http.request(config);
    return awaitGet;
  },
  // post请求
  async post(path: string, params?: any, head?: any, showMsg = true) {
    let config: any = { path, params, head };
    config.method = 'post';
    config.showMsg = showMsg;
    // config.baseURL = import.meta.env[params.webBaseUrl || 'VITE_BASE_URL']
    // config.baseURL = baseUrl
    const awaitPost = await http.request(config);
    return awaitPost;
  },
  async $post(path: string, params?: any, head?: any, showMsg = true) {
    let config: any = { path, params, head };
    config.method = 'post';
    config.showMsg = showMsg;
    config.data = params;
    config.head = {
      'Content-Type': 'application/x-www-form-urlencoded',
    };
    // config.baseURL = import.meta.env[params.webBaseUrl || 'VITE_BASE_URL']
    // config.baseURL = baseUrl
    const awaitPost = await http.request(config);
    return awaitPost;
  },
  // putt请求
  async put(path: string, params?: any, head?: any, showMsg = true) {
    let config: any = { path, params, head };
    config.method = 'put';
    config.showMsg = showMsg;
    // config.baseURL = import.meta.env[params.webBaseUrl || 'VITE_BASE_URL']
    const awaitPut = await http.request(config);
    return awaitPut;
  },
  // del请求
  async delete(path: string, params?: any, head?: any, showMsg = true) {
    let config: any = { path, params, head };
    config.method = 'delete';
    config.showMsg = showMsg;
    const awaitDel = await http.request(config);
    return awaitDel;
  },
  // post请求
  async upload(path: string, params?: any, head?: any, showMsg = true) {
    let config: any = { path, params, head };
    config.showMsg = showMsg;
    config.headers = {
      'Content-Type': 'multipart/form-data',
    };
    config.method = 'post';
    // config.baseURL = import.meta.env[params.webBaseUrl || 'VITE_BASE_URL']
    const awaitPost = await http.request(config);
    return awaitPost;
  },
  // blob下载流
  async blobDownload(path: string, params?: any, head?: any, showMsg = true) {
    let config: any = { path, params, head };
    config.headers = {
      'Content-Type': 'application/octet-stream;charset=UTF-8',
    };
    config.responseType = 'blob';
    config.method = 'get';
    config.showMsg = showMsg;
    config.baseURL = import.meta.env.VITE_BASE_URL;
    const awaitPost = await http.request(config);
    return awaitPost;
  },
  // 发起请求
  request(config: any) {
    let { path, method, head, params } = config;
    // const nodeEnv = process.env.NODE_ENV
    // const prefix = (nodeEnv === 'production') ? '/api' : '/api'
    // const prefix =
    config.baseURL = import.meta.env.VITE_BASE_URL;
    // config.baseURL = 'https://ccy-agent-admin-test.ccynice.com/admin-api/'
    // config.baseURL = import.meta.env[params.webBaseUrl || 'VITE_BASE_URL']
    config.headers = { ...config.headers, ...head };
    // 'Device-Id': 1,
    // 'Client-Type': 'SHOP_CONSOLE',

    // config.url =  `${path}`
    config.url = `${path}`;
    if (method === 'post' || method === 'put') {
      config.data = params;
      delete config.params;
    }
    // eslint-disable-next-line no-async-promise-executor
    return new Promise(async (resolve) => {
      try {
        const ret = await axios(config);

        if (ret.config?.responseType === 'blob') {
          if (Number(ret.status) === 200 || Number(ret.status) === 300) {
            ret.data.code = Number(ret.data.code);
            resolve(ret);
          }
        } else {
          if (Number(ret.data.code === 200)) {
            // 结果处理
            resolve(ret.data);
          } else {
            // 当查询成功,并且showMsg为true才显示消息提示
            if (
              ![200, 401, 600546, 600543, 600547, 600544].includes(
                ret.data.code,
              ) &&
              config.showMsg
            ) {
              // ElMessage.error(ret.data.msg || '操作异常')
              // message({ type: 'error', message: ret.data.msg || '操作异常' });
              Toast.show({ content: ret.data.msg || '操作异常' });
            }

            ret.data.code = Number(ret.data.code);
            // 结果处理
            resolve(ret.data);
          }
        }
      } catch (err) {
        if (window.location.pathname !== '/login') {
          Toast.show({ content: '服务异常' });
          setTimeout(() => {
            window.location.href = window.location.origin + '/login';
          }, 500);
        }
      }
    });
  },
};


